//
//  SimplePickerView.swift
//  ZKlink
//
//  Created by zkteco_iOS on 2024/9/26.
//

import UIKit
import SwiftEntryKit

class SimplePickerView: UIViewController {

    typealias ClickedClosure = (_ index: Int) -> Void
    var confirmActionBlock: ClickedClosure?
    var seletRow = 0
    var seletContent: String?
    
    var SourceData: [String?] = [] {
        didSet {
            let selectContents = SourceData
            if selectContents.count != 0 {
                self.seletContent = selectContents[0]
            }
            seletRow = 0
        }
    }
    
    lazy var pickerView: UIPickerView = {
        let pickerView = UIPickerView()
        pickerView.dataSource = self
        pickerView.delegate = self
        return pickerView
    }()
    
    lazy var cancelBtn: UIButton = {
        let button = UIButton()
        button.setTitle("取消", for: .normal)
        button.setTitleColor(color("#8D9198"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        button.addTarget(self, action: #selector(cancelBtnAction), for: .touchUpInside)
        button.backgroundColor = UIColor.clear
        return button
    }()
    
    lazy var confirmBtn: UIButton = {
        let button = UIButton()
        button.setTitle("确定", for: .normal)
        button.setTitleColor(color("#333333"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        button.addTarget(self, action: #selector(confirmBtnAction), for: .touchUpInside)
        button.backgroundColor = UIColor.clear
        return button
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.init(named: "DashboardBg")
        setupSubViews()
    }
    
    func setupSubViews(){
        view.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(0)
            make.left.equalToSuperview().offset(12)
            make.width.equalTo(80)
            make.height.equalTo(50)
        }
        
        view.addSubview(confirmBtn)
        confirmBtn.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(0)
            make.right.equalToSuperview().offset(-12)
            make.width.equalTo(80)
            make.height.equalTo(50)
        }

        let lineView = UIView()
        lineView.backgroundColor = .init(named: "E7E7E7Color")
        view.addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.top.equalTo(cancelBtn.snp.bottom)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        view.addSubview(pickerView)
        pickerView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-12)
            make.top.equalTo(lineView.snp.bottom)
            make.bottom.equalToSuperview()
        }
    }
    
    @objc func cancelBtnAction () {
        SwiftEntryKit.dismiss()
    }

    @objc func confirmBtnAction () {
        self.confirmActionBlock?(self.seletRow)
        SwiftEntryKit.dismiss()
    }
    
    // MARK: UISearchBarDelegat
    // TODO:这是返回每一Row的宽度
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return screenWidth
    }
    //TODO: 设置每一个Row的高度
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 40.0
    }

    // TODO:这是选择哪一个Row,调用的函数
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        seletContent = SourceData[row]
        seletRow = row
    }

    
}

extension SimplePickerView: UIPickerViewDelegate, UIPickerViewDataSource {
    // 返回每一个Row的文本
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return SourceData[row]
    }
    
    // 可以设置哪一行显示特定的样式
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        // 创建一个对象
        let specificView = UIView.init()
        specificView.frame = CGRect.init(x: 0, y: 0, width: screenWidth-65, height: 30)
        specificView.backgroundColor = UIColor.clear
        /**
         创建一个标题
         */
        let specificLable = UILabel.init(frame: CGRect.init(x: -10, y: 0, width: screenWidth-65, height: 30))
        specificLable.text = SourceData[row]
        specificLable.textColor = UIColor.init(named: "titleColor")
        specificLable.textAlignment = .center
//        specificLable.lineBreakMode = .byTruncatingTail
        specificView.addSubview(specificLable)
        return specificView
    }
    
    // 返回列表的组数
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    // 这是设置返回列表的个数
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return SourceData.count
    }
}

